package touro.mancala.slave;

import java.util.LinkedList;
import java.util.StringTokenizer;



public class Slave {
	
	LinkedList<Board> boards;
	int numMoves;
	int levels;
	
	public Slave() {
		levels = 4;
	}
	
	public Slave(int levels) {
		this.levels = levels;
	}
	
	private int scoreBoard(Board board) {
		if(board.getNumMoves() == numMoves+levels) {
			return board.getScore();
		}
		
		LinkedList<Board> boards = board.getChildrenBoards();
		int[] scores = new int[6];
		int index = 0;
		for (Board child : boards) {
			scores[index] = scoreBoard(child);
		}
		
		int max = -49;
		for(int score : scores) {
			max = score > max ? score : max;
		}
		return max;
	}

	public String getScore(String message){
		StringTokenizer getInd = new StringTokenizer(message, " ");

		int unique = Integer.parseInt(getInd.nextToken());
		String boardString = getInd.nextToken();
		
		int index = Integer.parseInt(getInd.nextToken());
		String active = getInd.nextToken();
		
		Board board = new Board(boardString, active, true);
		
		return unique + " " +index+" "+ evaluate(board, index);
	}
	
	private int evaluate(Board board, int index) {
		Board sownBoard = new Board(board);
		sownBoard.sow(index);
		numMoves = 1;
		
		return scoreBoard(sownBoard);
	}
	
}
